package com.ndp.fb.walle.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.exception.CheckedException;
import com.ndp.fb.exception.ResourceMsgManager;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.model.vo.OutputResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Title: ResponseUtils.java Description: ResponseUtils
 *
 * @author Sisi 创建时间 2014年7月8日 下午6:26:02
 */
public class ResponseUtils {

    /**
     * @param outputJson 待返回的Json字符串
     * @return ResponseEntity<String>
     * @description 将Json字符串返回到客户端，并设定Header为UTF-8
     * @author Sisi 创建时间 2014年7月8日 下午6:25:13
     */
    public static ResponseEntity<String> returnJsonWithUTF8(String outputJson) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        return new ResponseEntity<String>(outputJson, headers, HttpStatus.OK);
    }

    public static ResponseEntity<String> returnJsonWithUTF8(Object data) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        String outputJson = JSON.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
        return new ResponseEntity<>(outputJson, headers, HttpStatus.OK);
    }


    /**
     * 返回信息，已经国际化过
     *
     * @param errorType
     * @return
     */
    public static String getMsg(ErrorType errorType) {
        return getMsgByKey(errorType.getCode());
    }

    public static String getMsg(ErrorType errorType, Object... params) {
        return getMsgByErrorCode(errorType.getCode(), params);
    }

    /**
     * 根据errorCode后去对应的国际化的消息
     * @param errorCode
     * @return
     */
    public static String getMsgByKey(String errorCode) {
        return ResourceMsgManager.getResource(getLanguage(), errorCode);
    }

    /**
     * 根据errorCode后去对应的国际化的消息
     * @param errorCode
     * @return
     */
    public static String getMsgByErrorCode(String errorCode, Object... params) {
        return ResourceMsgManager.getResource(getLanguage(), errorCode, params);
    }


    /**
     * @param errorType errorType
     * @param data      data
     * @return OutputResult
     * @description 构造响应结果
     * @author Sisi
     * 创建时间 2014年7月10日 下午7:35:06
     */
    public static OutputResult bulidOutputResult(ErrorType errorType, Object data) {
        return bulidOutputResultWithPlaceholders(errorType, data, null);
    }

    /**
     * @param errorType errorType
     * @param data      data
     * @return OutputResult
     * @description 构造响应结果
     * @author Sisi
     * 创建时间 2014年7月10日 下午7:35:06
     */
    public static OutputResult bulidOutputResultWithPlaceholders(ErrorType errorType, Object data, Object... Placeholders) {
        OutputResult outputResult = new OutputResult();
        if (null != errorType) {
            outputResult.setFlag(CommonConstants.FAIL);
            String errorCode = errorType.getCode();
            String msg = null;
            if (Placeholders == null || Placeholders.length == 0) {
                msg = getMsgByKey(errorCode);
            } else {
                msg = getMsgByErrorCode(errorCode, Placeholders);
            }
            outputResult.setMsg(msg);
            outputResult.setErrorCode(errorCode);
        } else {
            outputResult.setFlag(CommonConstants.SUCCESS);
        }
        outputResult.setData(data);
        return outputResult;
    }


    /**
     * @param checkedException
     * @param data
     * @return
     */
    public static OutputResult bulidOutputResultException(CheckedException checkedException, Object data) {
        return bulidOutputResultExceptionWithPlaceholders(checkedException, data, null);
    }


    /**
     * @param checkedException
     * @param data
     * @return
     */
    public static OutputResult bulidOutputResultExceptionWithPlaceholders(CheckedException checkedException, Object data, Object... Placeholders) {
        OutputResult outputResult = new OutputResult();
        if (null != checkedException) {
            outputResult.setFlag(CommonConstants.FAIL);
            String errorCode = checkedException.getErrorCode();
            String msg = null;
            if (Placeholders == null || Placeholders.length == 0) {
                msg = getMsgByKey(errorCode);
            } else {
                msg = getMsgByErrorCode(errorCode, Placeholders);
            }
            outputResult.setMsg(msg);
            outputResult.setErrorCode(errorCode);
        } else {
            outputResult.setFlag(CommonConstants.SUCCESS);
        }
        outputResult.setData(data);
        return outputResult;
    }


    /**
     * 获取当前账号的语言
     *
     * @return
     */
    private static String getLanguage() {
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        if (user == null) {
            return "";
        } else {
            return user.getLanguage();
        }
    }
}
